package com.tzy.for10evalue.utils.localcache;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author tzy
 * @date 2022/3/21 16:06
 */
public class LockUtil {

    //条件和对应的锁
    private static final Map<String, Object> lockMap = new ConcurrentHashMap<>();

    /**
     * 根据不同的条件获取不同的锁,一个cacheName下的cacheKey只对应一个锁
     *
     * @param cacheKey
     * @return
     */
    public static Object getLock(String cacheName, String cacheKey) {
        Object lock = lockMap.get(cacheKey);
        if (lock == null) {
            synchronized (cacheName.intern()) {
                lock = lockMap.get(cacheKey);
                if (lock == null) {
                    lock = new Object();
                    lockMap.put(cacheKey, lock);
                }
            }
        }
        return lock;
    }
}
